//
//  UIColor+extensionColor.swift
//  CRMModule
//
//  Created by lazy on 17/4/6.
//  Copyright © 2017年 mechat. All rights reserved.
//

import UIKit

public extension UIColor {
    
    /// 16进制字符串转UIColor对象
    ///
    /// - Parameter hexString: 16进制字符串
    /// - Returns: UIColor对象
    public class func color(hexString: String) -> UIColor {
        var cString = hexString.trimmingCharacters(in:CharacterSet.whitespacesAndNewlines).uppercased()
        
        if (cString.hasPrefix("#")) {
            let index = cString.index(cString.startIndex, offsetBy:1)
            cString = cString.substring(from: index)
        }
        
        if (cString.characters.count != 6) {
            return UIColor.red
        }
        
        let rIndex = cString.index(cString.startIndex, offsetBy: 2)
        let rString = cString.substring(to: rIndex)
        let otherString = cString.substring(from: rIndex)
        let gIndex = otherString.index(otherString.startIndex, offsetBy: 2)
        let gString = otherString.substring(to: gIndex)
        let bIndex = cString.index(cString.endIndex, offsetBy: -2)
        let bString = cString.substring(from: bIndex)
        
        var r:CUnsignedInt = 0, g:CUnsignedInt = 0, b:CUnsignedInt = 0;
        Scanner(string: rString).scanHexInt32(&r)
        Scanner(string: gString).scanHexInt32(&g)
        Scanner(string: bString).scanHexInt32(&b)
        
        return UIColor(red: CGFloat(r) / 255.0, green: CGFloat(g) / 255.0, blue: CGFloat(b) / 255.0, alpha: CGFloat(1))
    }
    
    /// 便利生成rgb UIColor对象
    ///
    /// - Parameters:
    ///   - r: red值
    ///   - g: green值
    ///   - b: blue值
    /// - Returns: UIColor对象
    public class func color(r:CGFloat, g:CGFloat, b:CGFloat) -> UIColor {
        return UIColor(red: r/255.0, green: g/255.0, blue: b/255.0, alpha: 1.0)
    }
    
    private func color(r: CGFloat, g: CGFloat, b: CGFloat) -> UIColor {
        return UIColor(red: r/255.0, green: g/255.0, blue: b/255.0, alpha: 1.0)
    }
    
    public class var charcoalGrey: UIColor {
        get{
            return UIColor(red: 52/255.0, green: 63/255.0, blue: 75/255.0, alpha: 1.0)
        }
    }
    
    public class var steelGrey: UIColor {
        get{
            return UIColor(red: 118/255.0, green: 125/255.0, blue: 133/255.0, alpha: 1.0)
        }
    }
    
    public class var clearBlue: UIColor {
        get{
            return UIColor(red: 44/255.0, green: 124/255.0, blue: 255/255.0, alpha: 1.0)
        }
    }
    
    public class var backgroundGrey: UIColor {
        get{
            return UIColor(red: 245/255.0, green: 245/255.0, blue: 245/255.0, alpha: 1.0)
        }
    }
    
    public class var separatorGrey: UIColor {
        get{
            return UIColor(red: 226/255.0, green: 226/255.0, blue: 226/255.0, alpha: 1.0)
        }
    }
    
    public class var coolGrey: UIColor {
        get{
            return UIColor(red: 150/255.0, green: 159/255.0, blue: 170/255.0, alpha: 1.0)
        }
    }
    
    public class var randomColor:UIColor {
        get{
            return UIColor(red: CGFloat(arc4random()%256)/255.0, green: CGFloat(arc4random()%256)/255.0, blue: CGFloat(arc4random()%256)/255.0, alpha: 1.0)
        }
    }
    
    public class var cloudyBlue: UIColor {
        get{
            return color(r: 204, g: 214, b: 221)
        }
    }
    
    public class var opacityBackgroundBlack: UIColor {
        get{
            return UIColor(red: 34/255.0, green: 42/255.0, blue: 52/255.0, alpha: 0.4)
        }
    }
}



